class Chart
  require 'rexml/document'
  
  def initialize(series_count)
    @license_key = "CTQIP-JPUUEOHMZ0B6SVMYWHM5SXBL"
    @category_array = Array.new
    @series_array = Array.new(series_count)
    @series_color_array = Array.new
    @link_array = Array.new
    @draw_array = Array.new
    i=0
    while i<series_count do
      @series_array[i] = Array.new
      i+=1
    end
    i=0
  end  
  attr_reader :chart_type,:link_data,:axis_category,:chart_transition,:axis_value,:legend,:chart_note
  attr_writer :chart_type,:link_data,:axis_category,:chart_transition,:axis_value,:legend,:chart_note
  
  def add_category(value)
    @category_array << value
  end

  def add_link_area(x,y,w,h,t,p,u,tt)
    @link_array << [x,y,w,h,t,p,u,tt]
  end

  def add_draw_area(type,x,y,w,h,f,a)
    @draw_array << [type,x,y,w,h,f,a]
  end
  
  def add_series_value(series,value,attribute_hash)
    @series_array[series] << [value,attribute_hash]
    if value == "Flickr"
      @series_color_array << "339900"
    end
    if value == "Slideshare"
      @series_color_array << "FF9900"
    end
    if value == "Twitter"
      @series_color_array << "FF0000"
    end
    if value == "Youtube"
      @series_color_array << "000099"
    end
  end
  
  def to_xml
    rootElement = REXML::Element.new('chart')
    doc = REXML::Document.new()
    doc.add_element(rootElement)    
    rootElement.add_element('license').add_text("#{@license_key}")
    rootElement.add_element('chart_type').add_text("#{@chart_type}")
    rootElement.add_element('link_data',@link_data) unless @link_data==nil
    rootElement.add_element('axis_category',@axis_category) unless @axis_category==nil
    rootElement.add_element('chart_transition',@chart_transition) unless @chart_transition==nil
    rootElement.add_element('axis_value',@axis_value) unless @axis_value==nil
    rootElement.add_element('legend',@legend) unless @legend==nil
    rootElement.add_element('chart_note',@chart_note) unless @chart_note==nil
    rootElement.add_element('chart_rect',{'height'=>'200','y'=>'100'})
    seriesColorElement = rootElement.add_element('series_color')
    @series_color_array.each do |color|
      seriesColorElement.add_element('color').add_text("#{color}")
    end

    chartDataElement = rootElement.add_element('chart_data')

    linkElement = rootElement.add_element('link')
    @link_array.each do |link|
      linkElement.add_element('area',{'x'=>link[0],'y'=>link[1],'width'=>link[2],'height'=>link[3],'target'=>link[4],'priority'=>link[5],'url'=>link[6],'tooltip'=>link[7]})
    end

    drawElement = rootElement.add_element('draw')
    @draw_array.each do |draw|
      if draw[0] == "rect"
        drawElement.add_element(draw[0],{'x'=>draw[1],'y'=>draw[2],'width'=>draw[3],'height'=>draw[4],'fill_color'=>draw[5], 'fill_alpha'=>draw[6]})
      else
        drawElement.add_element(draw[0],{'x'=>draw[1],'y'=>draw[2],'width'=>draw[3],'size'=>draw[4],'color'=>draw[5],'h_align'=>'center','font'=>'Verdana'}).add_text("#{draw[6]}")
      end
    end

    labelRowElement = chartDataElement.add_element('row')
    @category_array.each do |category|
      labelRowElement.add_element('string').add_text("#{category}")
    end
    i=0
    while i<@series_array.size do
      dataRowElement = chartDataElement.add_element('row')
      dataRowElement.add_element('string').add_text("#{@series_array[i][0]}")
      j=1
      while j<@series_array[i].size do
        dataRowElement.add_element('number',@series_array[i][j][1]).add_text("#{@series_array[i][j][0]}")
        j+=1
      end
      i+=1
    end
    return doc.to_s
  end
end
